
// <copyright file="AbstractEnumeratorDataReader.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The abstract enumerator data reader.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data.Common
{
    using System.Collections.Generic;

    /// <summary>
    /// The abstract enumerator data reader.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public abstract class AbstractEnumeratorDataReader<T> : AbstractDataReader
    {
        #region Fields

        /// <summary>
        /// The enumerator.
        /// </summary>
        private IEnumerator<T> enumerator;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractEnumeratorDataReader{T}"/> class.
        /// </summary>
        protected AbstractEnumeratorDataReader()
        {
            this.SequenceNumber = 0;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the sequence number.
        /// </summary>
        public long SequenceNumber { get; private set; }

        #endregion Public Properties

        #region Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        protected T current { get; private set; }

        #endregion Properties

        #region Public Methods and Operators

        /// <summary>
        /// The set enumerator.
        /// </summary>
        /// <param name="enumerator">
        /// The enumerator.
        /// </param>
        /// <param name="resetSequenceNumber">
        /// The reset sequence number.
        /// </param>
        public void SetEnumerator(IEnumerator<T> enumerator, bool resetSequenceNumber)
        {
            this.enumerator = enumerator;
            this.current = default(T);
            if (resetSequenceNumber)
            {
                this.SequenceNumber = 0;
            }
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The read protected.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        protected override bool readProtected()
        {
            if (this.enumerator.MoveNext())
            {
                this.SequenceNumber++;
                this.current = this.enumerator.Current;
                return true;
            }

            this.enumerator.Dispose();
            return false;
        }

        #endregion Methods
    }
}